package com.xqls.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.xqls.bean.CostBean;
import com.xqls.bean.FlowMeterBean;
import com.xqls.bean.UserBean;
import com.xqls.biz.CompanyBiz;
import com.xqls.biz.CostBiz;
import com.xqls.biz.DictionaryBiz;
import com.xqls.biz.FlowMeterBiz;
import com.xqls.biz.FlowRecordBiz;
import com.xqls.biz.LoginBiz;
import com.xqls.dal.model.FlowMeter;
import com.xqls.dal.model.FlowRecord;
import com.xqls.redis.Constants;
import com.xqls.redis.RedisCache;
import com.xqls.redis.RedisKeyEnum;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DateFormatter;
import com.xqls.util.DictionaryEnums;
import com.xqls.util.GetKeyByValue;

@Controller
@RequestMapping("/flowmeter")
public class FlowMeterController {

	@Autowired
	private FlowMeterBiz biz;
	@Autowired
	private CompanyBiz companyBiz;

	@Autowired
	private CostBiz costBiz;
	
	@Autowired 
	private DictionaryBiz dictionaryBiz;
	
	@Autowired
	private RedisCache<String, Object> redisCache;

	
	@Autowired private LoginBiz loginBiz;
	
	
	
	@Autowired
	private FlowRecordBiz flowRecordBiz;

	@RequestMapping(value = "/index")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("flowmeter/flowmeterIndex");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.COMPANY_TYPE.getCode());
//		boolean isHave = PurviewUtil.getIsHavePurview(DictionaryEnums.ADD_BOTTON);
//		if(isHave){
//			view.addObject("addButton", "yes");
//		}
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}
	
	@RequestMapping(value = "/flowIndex")
	public ModelAndView flowIndex() {
		ModelAndView view = new ModelAndView("flowmeter/flowIndex");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}
	
	
	
	@RequestMapping(value = "/flowmeterListData")
	@ResponseBody
	public CommonResult flowmeterListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = biz.getFlowByPage(commonPage,request,response);
		if (result.getIsSuccess()) {
			result.setData(commonPage);
		}
		return result;
	}
	
	@RequestMapping(value = "/flowListData")
	@ResponseBody
	public CommonResult flowListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = biz.getByPage(commonPage,request,response);
		if (result.getIsSuccess()) {
			result.setData(commonPage);
		}
		return result;
	}
	
	@RequestMapping(value = "/flowedit/{id}")
	public ModelAndView flowedit(@PathVariable(value = "id") Integer id) {
		ModelAndView view = new ModelAndView("flowmeter/flowEdit");
		Subject subject = SecurityUtils.getSubject();
		String username = subject.getPrincipal().toString();
		UserBean user = (UserBean) redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_SESSION, username));
		String url=user.getBlockId()+";";
		view.addObject("companyList", companyBiz.queryByBlock(url,DictionaryEnums.INTAKE.getCode()));
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		if (id != null && !"".equals(id)) {
			FlowMeterBean bean = biz.queryByKey(id);
			bean.setCloseMonthString(DateFormatter.format(bean.getCloseMonth()));
			bean.setRecordTimeString(DateFormatter.format(bean.getRecordTime()));
			if (bean != null) {
				view.addObject("bean", bean);
			}
		}
		return view;
	}

	@RequestMapping(value = "/add/{id}")
	public ModelAndView edit(@PathVariable(value = "id") Integer id) {
		ModelAndView view = new ModelAndView("flowmeter/flowmeterAdd");
		Subject subject = SecurityUtils.getSubject();
		String username = subject.getPrincipal().toString();
		UserBean user = (UserBean) redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_SESSION, username));
		String url=user.getBlockId()+";";
		view.addObject("companyList", companyBiz.queryByBlock(url,DictionaryEnums.INTAKE.getCode()));
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}

	@RequestMapping(value = "/remove")
	@ResponseBody
	public String remove(@RequestBody List<Integer> ids) {
		return JSONObject.toJSONString(biz.deleteFlowRole(ids));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(FlowMeterBean bean) {
		return JSONObject.toJSONString(biz.addFlowRole(bean));
	}

	
	@RequestMapping("/flowsave")
	@ResponseBody
	public String flowsave(FlowMeterBean bean) {
		return JSONObject.toJSONString(biz.editFlowRole(bean));
	}
	
//	@RequestMapping("/selflowtime")
//	@ResponseBody
//	public String selflowtime(FlowMeterBean bean) {
//		return JSONObject.toJSONString(biz.selflow(bean));
//	}
	
	@RequestMapping(value = "/Invert")
	@ResponseBody
	public String Invert(String closeMonth,String deviceNo ){
		FlowMeter b=null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			b = biz.queryByClose(closeMonth,deviceNo);
			b.setRecordTimeString(DateFormatter.format(b.getRecordTime()));
			if (b != null) {
				map.put("AccumuLate", b.getAccumuLate());
				map.put("RecordTime", b.getRecordTimeString());
				map.put("FlowId", b.getFlowId());
			}
		} catch (Exception e){
		}
		return JSONObject.toJSONString(map);
	}

	@RequestMapping(value = "/cost")
	private ModelAndView flowcostindex() {
		ModelAndView view = new ModelAndView("flowmeter/costIndex");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}

	@RequestMapping(value = "/costListData")
	@ResponseBody
	public CommonResult costListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = biz.selectBySmonth(commonPage,request,response);
		if (result.getIsSuccess()) {
			result.setData(commonPage);
		}
		return result;
	}

	@RequestMapping(value = "/costedit/{id}/{date}")
	public ModelAndView costedit(@PathVariable(value = "id") Integer id,@PathVariable(value = "date") String date) {
		ModelAndView view = new ModelAndView("flowmeter/costEdit");
		List<Map<String, Object>> bean = biz.queryselectflowcost(id, date);
		view.addObject("bean", bean);
		return view;
	}

	@RequestMapping("/costadd")
	@ResponseBody
	public String costsave(CostBean bean) {
		return JSONObject.toJSONString(costBiz.add(bean));
	}

	@RequestMapping(value = "/costList")
	@ResponseBody
	public CommonResult costList(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = costBiz.getAll(commonPage,request,response);
		if (result.getIsSuccess()) {
			result.setData(commonPage);
		}
		return result;
	}

	@RequestMapping(value = "/costUpdate/{id}")
	public ModelAndView editupdate(@PathVariable(value = "id") Integer id) {
		ModelAndView view = new ModelAndView("flowmeter/costUpdate");
		view.addObject("companyList", companyBiz.getList());

		//CostBean cost=costBiz.selectByKey(id);
		//cost.setCloseMothString(DateFormatter.format(cost.getCloseMoth()));
		//view.addObject("cost", cost);

		CostBean cost = costBiz.selectByKey(id);
		cost.setCloseMothString(DateFormatter.format(cost.getCloseMoth()));
		view.addObject("cost", cost);
		return view;
	}

	@RequestMapping("/costupdate")
	@ResponseBody
	public String updatecostsave(CostBean bean) {
		return JSONObject.toJSONString(costBiz.update(bean));
	}

	@RequestMapping(value = "/getExcelByMap/{params}")
	public void getExcel(HttpServletRequest request,HttpServletResponse response,@PathVariable(value = "params") String params) {
		JSONObject jsonObject=JSONObject.parseObject(params);
		this.biz.getListByMap(request, response, jsonObject);
	}
	
	@RequestMapping(value = "/selflow")
	@ResponseBody
	public String selflow( String closeMonth,String deviceNo){
		FlowRecord b=null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			b=flowRecordBiz.selflow(deviceNo, closeMonth);
			if(b!=null){
				map.put("AccumuLate", b.getCumulativeFlow());
			}
		} catch (Exception e){
		}
		return JSONObject.toJSONString(map);
	}

}
